home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / printing / rlpr-1.000 / rlpr-1 / rlpr-1.13 / rlpr-dbfile.c < prev    next >
C/C++ Source or Header  |  1996-06-30  |  4KB  |  114 lines

  1. /* filename: rlpr-dbfile.c
  2.  * project: rlpr
  3.  * author: meem  --  meem@sherilyn.wustl.edu
  4.  * version: $Id: rlpr-dbfile.c,v 1.4 1996/04/28 21:51:21 meem Exp $
  5.  * contents: procedures for the rlpr database file
  6.  * 
  7.  * Time-stamp: <1996/04/27 17:17 -- meem@sherilyn.wustl.edu>
  8.  */
  9.  
  10. /* copyright (c) 1996 meem, meem@gnu.ai.mit.edu
  11.  *
  12.  * This program is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 1, or (at your option)
  15.  * any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20.  * General Public License for more details.
  21.  */
  22.  
  23. #include <stdio.h>              /* for FILE*, etc */
  24. #include <string.h>
  25. #include <stdlib.h>             /* for getenv() and malloc() */
  26. #include "rlpr-common.h"
  27. #include "rlpr-dbfile.h"
  28.  
  29. static char *dbfilename = NULL;
  30. static FILE *dbp;
  31.  
  32. void opendbfile(void) {
  33.   char *home;
  34.  
  35.   if (!(home = getenv("HOME")))
  36.     rlpr_fatal("getenv: $HOME not set!");
  37.  
  38.   dbfilename = malloc(strlen(home) + strlen(DBNAME) + 2);
  39.   sprintf(dbfilename, "%s/%s", home, DBNAME);
  40.  
  41.   if (!(dbp = fopen(dbfilename, "r"))) 
  42.     rlpr_fatal("unable to open %s: %s", dbfilename, ERRNO);
  43. }
  44.  
  45. void closedbfile(void) {
  46.   if (fclose(dbp) < 0)
  47.     rlpr_fatal("unable to close %s: %s", dbfilename, ERRNO);
  48. }
  49.  
  50. char * getqfromhost(char *printhost) {
  51.   char *queue;                            /* what this returns */
  52.   opendbfile();
  53.  
  54.   if ((queue = db_search(printhost, FINDQ)) == NULL)
  55.     rlpr_fatal("cannot resolve printqueue for host, check %s",dbfilename);
  56.  
  57.   closedbfile();
  58.   return queue;
  59. }
  60.  
  61. char * gethostfromq(char *queue) {
  62.   char *printhost;                        /* returned by db_search */
  63.   opendbfile();  
  64.  
  65.   if ((printhost = db_search(queue, FINDHOST)) == NULL)
  66.     rlpr_fatal("cannot resolve host for printqueue, check %s",dbfilename);
  67.  
  68.   closedbfile();                          /* close database file */
  69.   return printhost;
  70. }
  71.  
  72. /* main engine */
  73.  
  74. char * db_search(char *sstr, int type) {
  75.   char line[DB_LINE_LEN];
  76.   char *cloc, *hloc, *qloc;                 /* ptr to colon, host, q in line */
  77.   char *result = NULL;                      /* our result */
  78.   char *linep = line;                       /* pointer to line */
  79.   int sz;                                   /* ubiquitous size variable */
  80.  
  81.   while ((fgets(line, DB_LINE_LEN, dbp))) {
  82.     if (*line == '\n' || *line == '#') continue;    /* comment line */
  83.     else if (!(cloc = strchr(line, ':'))) continue; /* invalid line */
  84.  
  85.     if (type == FINDQ) {
  86.       strlower(sstr);                       /* lowercase the hostname */
  87.  
  88.       if ((hloc = strstr(line, sstr)) == NULL) continue;
  89.       if ((strchr(" \t:", hloc[strlen(sstr)])) == NULL) continue;
  90.       if (hloc >= cloc) continue;
  91.  
  92.       while (*(++cloc) == ' ');             /* chop off whitespace */
  93.       sz = strcspn(cloc, "\n! ");           /* grab next "word" */
  94.       result = malloc(sz + 1);
  95.       strncpy(result, cloc, sz);
  96.       result[sz] = '\0';                    /* add null byte */
  97.  
  98.     } else /* FINDHOST */ {
  99.  
  100.       if ((qloc = strstr(cloc, sstr)) == NULL) continue;
  101.       if ((strchr(" !\n", qloc[strlen(sstr)])) == NULL) continue;
  102.  
  103.       while (*linep == ' ') linep++;        /* chop off whitespace */
  104.       sz = strcspn(linep, " :");            /* grab next word */
  105.       if (result) free(result);             /* old failed attempt? */
  106.       result = malloc(sz + 1);
  107.       strncpy(result, linep, sz);
  108.       result[sz] = '\0';                /* add null byte */
  109.       if (qloc[strlen(sstr)] == '!') return result;
  110.     }
  111.   }
  112.   return result;
  113. }
  114.